import Vue from 'vue';
import Regist from './regist.vue';
import {
  registAxios
} from 'network/Profile.js';


// 1.创建组件构造器
const RegistConstructor = Vue.extend(Regist)

function uesrRegist() {
  // 创建新的组件对象
  const registDom = new RegistConstructor({
    // 挂载el
    el: document.createElement('div'),

    // 用来注册成功后让组件消失
    data() {
      return {
        isShow: true
      }
    },
    methods: {
      regist() {
        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;

        // 对着两个进行判空 非空才提交
        if (!(username || password)) {
          this.$toast2('不能为空', 2000)
          return 0
        }

        registAxios({
          url: 'AddUsernameServlet',
          params: {
            username: username,
            password: password
          }
        }).then(res => {
          // servlet返回的数据
          switch (res.data) {
            case "same":
              this.$toast2('用户名已经被使用', 2000)
              break
            case "success":
              this.$toast2('注册成功', 2000)
              this.isShow = false;
              break
            case "fail":
              this.$toast2('服务器错误', 2000)
              break
          }
        }, err => {
          console.log('hello');
          this.$toast2(err, 2000)
        })
      }
    },


  })
  document.body.appendChild(registDom.$el);
}

// 注册为全局函数
function registyUserRegist() {
  Vue.prototype.$regist = uesrRegist
}
export default registyUserRegist;
